package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.db.UserApi;
import com.tanhua.dubbo.api.mapper.UserMapper;
import com.tanhua.model.db.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 用户服务提供者
 */
@DubboService
@Transactional
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;

    /**
     * 通过手机号码查询用户
     *
     * @param phone
     * @return
     */
    @Override
    public User findByMobile(String phone) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", phone);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 添加用户
     *
     * @param loginUser
     * @return
     */
    @Override
    public Long add(User loginUser) {
        userMapper.insert(loginUser);
        return loginUser.getId();
    }

    /**
     *  更新用户手机号码
     * @param phone
     */
    @Override
    public void updatePhone(Map<String, String> phone) {
        User user = new User();
        user.setMobile(phone.get("phone"));
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getId,phone.get("userid"));
        userMapper.update(user,qw);
    }

    /**
     *  更新环信账号
     * @param hxuser
     */
    @Override
    public void update(User hxuser) {
        userMapper.updateById(hxuser);
    }

    /**
     *  通过环信用户名查询
     * @param huanxinId
     * @return
     */
    @Override
    public User findByHxId(String huanxinId) {

        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getHxUser, huanxinId);

        return userMapper.selectOne(qw);
    }
}
